//
//  ViewController.m
//  网络获取图片
//
//  Created by 胡先剑 on 16/3/22.
//  Copyright © 2016年 胡先剑. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityView;

@property (nonatomic,strong) NSArray *arr;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.arr = @[@"http://www.apple.com.cn/mac-pro/images/static/processor.jpg",@"http://www.apple.com.cn/mac-pro/images/static/memory.jpg",@"http://www.apple.com.cn/mac-pro/images/static/graphics.jpg",@"http://www.apple.com.cn/mac-pro/images/static/storage.jpg",@"http://www.apple.com.cn/mac-pro/images/static/thermal.jpg",@"http://www.apple.com.cn/mac-pro/images/static/fan.jpg",@"http://www.apple.com.cn/mac-pro/images/static/expansion.jpg",@"http://www.apple.com.cn/mac-pro/images/static/wifi.jpg",@"http://www.apple.com.cn/mac-pro/images/static/design.jpg",@"http://www.apple.com.cn/mac-pro/images/static/comingsoon.jpg"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)btn1Pressed:(id)sender
{
    
    [self.activityView startAnimating];
    [self performSelectorInBackground:@selector(image2Info) withObject:nil];
    
}
- (IBAction)btn2Pressed:(id)sender
{
    [self.activityView startAnimating];
    int width = 100;
    int screenWidth = [UIScreen mainScreen].bounds.size.width;
    int lineCount = screenWidth / width;
    
    for (int i=0; i<self.arr.count; i++)
    {
        
        int a = i / lineCount;
        int b = i % lineCount;
        int x = width * b + 20;
        int y = width * a + 200;
        
        
        [self image1Info:self.arr[i] andX:x andY:y];
    }
    
}
- (IBAction)btn3Pressed:(id)sender
{
    
    [self.activityView startAnimating];
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_async(queue, ^{
        dispatch_group_t group = dispatch_group_create();
        NSMutableArray *mArrImage = [NSMutableArray array];
        NSMutableArray *mArrStr = [NSMutableArray array];
        for (int i=0; i<self.arr.count; i++)
        {
            __block UIImage *image = [[UIImage alloc] init];
            __block NSString *str = self.arr[i];
            [mArrImage addObject:image];
            [mArrStr addObject:str];
        }
        for (int i=0; i<self.arr.count; i++)
        {
            dispatch_group_async(group, queue, ^{
                mArrImage[i] = [self image3Info:mArrStr[i]];
            });
        }
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            int width = 100;
            int screenWidth = [UIScreen mainScreen].bounds.size.width;
            int lineCount = screenWidth / width;
            for (int i=0; i<mArrStr.count; i++)
            {
                int a = i / lineCount;
                int b = i % lineCount;
                int x = width * b + 20;
                int y = width * a + 200;
                
                UIImageView *imgView = [[UIImageView alloc] initWithImage:mArrImage[i]];
                imgView.frame = CGRectMake(x, y, 100, 100);
                [self.view addSubview:imgView];
            }
            [self.activityView stopAnimating];
        });
    });
}
- (UIImage *)image3Info:(NSString *)urlStr
{
    // 网址对象
    NSURL *url = [NSURL URLWithString:urlStr];
    // 获取数据
    NSData *data = [NSData dataWithContentsOfURL:url];
    // 图像对象
    return [UIImage imageWithData:data];
}

- (void)image1Info:(NSString *)urlStr andX:(int)x andY:(int)y
{
    [self.activityView startAnimating];
    dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
    
    dispatch_async(queue, ^{
        // 网址对象
        NSURL *url = [NSURL URLWithString:urlStr];
        // 获取数据
        NSData *data = [NSData dataWithContentsOfURL:url];
        // 图像对象
        UIImage *image = [UIImage imageWithData:data];
        dispatch_async(dispatch_get_main_queue(), ^{
            // 图像视图
            UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
            imgView.frame = CGRectMake(x, y, 100, 100);
            // 添加到视图
            [self.view addSubview:imgView];
            [self.activityView stopAnimating];
            
        });
    });
}

- (void)image2Info
{
    NSMutableArray *mArr = [NSMutableArray array];
    for (int i=0; i<self.arr.count; i++)
    {
        // 网址对象
        NSURL *url = [NSURL URLWithString:self.arr[i]];
        // 获取数据
        NSData *data = [NSData dataWithContentsOfURL:url];
        // 图像对象
        UIImage *image = [UIImage imageWithData:data];
        [mArr addObject:image];
    }
    int width = 100;
    int screenWidth = [UIScreen mainScreen].bounds.size.width;
    int lineCount = screenWidth / width;
    

    for (int j=0; j<mArr.count; j++)
    {
        int a = j / lineCount;
        int b = j % lineCount;
        int x = width * b + 20;
        int y = width * a + 200;
        UIImageView *imageView = [[UIImageView alloc] init];
        [imageView performSelectorOnMainThread:@selector(setImage:) withObject:mArr[j] waitUntilDone:NO];
        imageView.frame = CGRectMake(x, y, width, width);
        [self.view addSubview:imageView];
    }
    [self.activityView performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];
}

@end
